<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
	public function _initialize() {
       $img = D('Img')->order("sort desc")->select();
       if(empty($img)){
       	$this->error('没有轮播图');
       }
       $this->assign('list',$img);

         //联系我们
    $contact = D('Contact')->order('id desc')->find();
    $contact['content'] = html_entity_decode($contact['content']);
      if(empty($contact)){
      	$this->error('没有添加关于我们');
      }	
    $this->assign('contact',$contact);
	}



    public function index(){
      //关于我们
      $about = D('About')->order('id desc')->find();
      $about['content'] = html_entity_decode($about['content']);
      if(empty($about)){
      	$this->error('没有添加关于我们');
      }	
    $this->assign('about',$about);

 //产品案例
    $case = D('case')->order('sort asc')->limit(0,4)->select();
    if(empty($case)){
        $this->error('没有添加案例');
      } 
    $this->assign('case',$case);

   //新闻动态
   $news = D('news')->order('createtime desc')->limit(0,3)->select();
      if(empty($news)){
        $this->error('没有添加新闻动态');
      } 
    $this->assign('news',$news);
    $this->display();
 }



 public function message(){
 	extract($_POST);
 	if(trim($name)==""){
 		echo json_encode(array("status"=>"0","msg"=>"用户名不能位空!"));
 		exit;
 	}
 	if(strlen($name) >40 ){
 		echo json_encode(array("status"=>"0","msg"=>"用户名过长!"));
 		exit;
 	}
 	$namelist = D('Messages')->where("name='".$name."'")->find();
 	if(!empty($namelist) || isset($namellist)){
 		echo json_encode(array("status"=>"0","msg"=>"用户名已存在!"));
 		exit;
 	}
 	$res = "/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/";
 	if(!preg_match($res,$email)){
		echo json_encode(array("status"=>"0","msg"=>"电子邮箱不正确!"));
 		exit;
 	}
  	$emaillist = D('Messages')->where("email='".$email."'")->find();
 	if(!empty($emaillist) || isset($emaillist)){
 		echo json_encode(array("status"=>"0","msg"=>"邮箱已存在!"));
 		exit;
 	}	
 	if(trim($content) == ""){
 		echo json_encode(array("status"=>"0","msg"=>"内容不能位空!"));
 		exit;
 	}

 	$data['name'] = $name;
 	$data['content'] = $content;
 	$data['email'] = $email;
 	$data['createtime'] = date('Y-m-d H:i:s');
 	$res = D('Messages')->add($data);
 	if($res){
 		echo json_encode(array("status"=>"1","msg"=>"留言成功，感谢您的留言，亲!"));
 		exit;
 	}else{
 	 	echo json_encode(array("status"=>"0","msg"=>"留言失败，请重新尝试吧!"));
 		exit;	
 	}
 }
}